﻿/*
Copyright (c) 2008 William Duff

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LiveUpload.Facebook
{
    public partial class FacebookAlbumRadioButton : RadioButton
    {
        #region Constructors

        /// <summary>
        /// Constructs a new FacebookAlbumRadioButton to represent an album that can be selected
        /// as part of the upload process.
        /// </summary>
        public FacebookAlbumRadioButton()
        {
            InitializeComponent();

            // Radio buttons don't handle double clicks by default, so we set the style manually.
            SetStyle(ControlStyles.StandardClick, true);
            SetStyle(ControlStyles.StandardDoubleClick, true);

            // Set the default value for the ComboBox and THEN wire-up the IndexChanged handler.
            this.facebookAlbumVisibilityComboBox.SelectedIndex = 0;
            this.facebookAlbumVisibilityComboBox.SelectedIndexChanged += new EventHandler(this.childControl_Click);
        }

        #endregion

        #region Private Fields

        private Album album;
        private const int PADDING = 10;

        #endregion

        #region Public Fields

        /// <summary>
        /// The album that this radio button represents.
        /// </summary>
        public Album Album
        {
            get 
            {
                if (this.album != null)
                {
                    return this.album;
                }
                else
                {
                    // If the album is null, return an uncreated album.
                    return new Album(this.facebookAlbumName.Text, string.Empty, string.Empty, GetAlbumVisibility());
                }
            }
            set
            {
                if (this.album != value)
                {
                    this.album = value;
                    OnAlbumChanged(new EventArgs());
                }
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// This event is fired when the album associated with this radio button is changed.
        /// </summary>
        private void OnAlbumChanged(EventArgs eventArgs)
        {
            if (this.album != null)
            {
                // Set a tool tip on the name label in case it auto-ellipsises.
                this.facebookAlbumToolTip.Active = true;
                this.facebookAlbumToolTip.SetToolTip(this.facebookAlbumName, this.album.Name);

                // Display the album name.
                this.facebookAlbumName.Text = this.album.Name;
                this.facebookAlbumName.Show();

                // Display the number of photos in the album.
                this.facebookAlbumSize.Text = 
                    this.album.Size == 1 ? this.album.Size.ToString() + " photo" : 
                    this.album.Size.ToString() + " photos";

                // Asynchronously load the user's profile picture.
                if (this.album.CoverPhoto != null && !string.IsNullOrEmpty(this.album.CoverPhoto.SrcSmall))
                {
                    this.facebookAlbumCoverPicture.LoadAsync(this.album.CoverPhoto.SrcSmall);
                }

                // Hide the visibility option.
                this.facebookAlbumVisibilityPanel.Visible = false;
                this.facebookAlbumVisibilityPictureBox.Visible = false;
                this.facebookAlbumVisibility.Visible = false;
                this.facebookAlbumVisibilityComboBox.Visible = false;

                // Update the UI.
                LayoutAlbumInformation();
            }
        }

        /// <summary>
        /// Resizes and realigns the album name and size labels.
        /// </summary>
        private void LayoutAlbumInformation()
        {
            // Allow the album name to be one or two lines tall depending on how long it is.
            if (this.facebookAlbumName.PreferredWidth < this.facebookAlbumName.Width)
            {
                this.facebookAlbumName.Height = 15;
            }
            else
            {
                this.facebookAlbumName.Height = 32;
            }

            // Middle-align the album name and size.
            this.facebookAlbumName.Top = Math.Max(PADDING, PADDING + this.facebookAlbumCoverPicture.Height/2 - this.facebookAlbumName.Height);
            this.facebookAlbumSize.Top = this.facebookAlbumName.Bottom;
        }

        /// <summary>
        /// This event is fired when a child control is clicked on.
        /// </summary>
        private void childControl_Click(object sender, EventArgs e)
        {
            OnClick(e);

            // Make sure this radio button gets selected as a result of the click.
            Select();
        }

        /// <summary>
        /// This event is fired when a child control is double clicked on.
        /// </summary>
        private void childControl_DoubleClick(object sender, EventArgs e)
        {
            OnDoubleClick(e);
        }

        /// <summary>
        /// This event is fired when a mouse enters the area of a child control.
        /// </summary>
        private void childControl_MouseEnter(object sender, EventArgs e)
        {
            OnMouseEnter(e);
        }

        /// <summary>
        /// This event is fired when this radio button is selected or deselected.
        /// </summary>
        private void FacebookAlbumRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            // If the album is null, this is the default "Create new album" radio button.
            if (this.Checked && this.album == null)
            {
                // Hide the current name of the album.
                this.facebookAlbumName.Hide();
                this.facebookAlbumNameTextBox.Enabled = true;
                this.facebookAlbumVisibilityComboBox.Enabled = true;
            }
            else if (this.facebookAlbumNameTextBox.Visible)
            {
                // Hide the text box and go back to showing the current name of the album.
                //this.facebookAlbumNameTextBox.Hide();
                //this.facebookAlbumName.Show();
                this.facebookAlbumNameTextBox.Enabled = false;
                this.facebookAlbumVisibilityComboBox.Enabled = false;
            }
        }

        /// <summary>
        /// This event is fired when the user changes the name of the album.
        /// </summary>
        private void facebookAlbumNameTextBox_TextChanged(object sender, EventArgs e)
        {
            // Keep the name of the album and the text in the text box synchronized.
            this.facebookAlbumName.Text = this.facebookAlbumNameTextBox.Text;
        }

        /// <summary>
        /// This event is fired when the cover picture of the album is loaded.
        /// </summary>
        private void facebookAlbumCoverPicture_LoadCompleted(object sender, 
            AsyncCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                // Resize the radio button and picture box depending on the size of the Facebook image.
                this.facebookAlbumCoverPicture.Top = PADDING;
                this.facebookAlbumCoverPicture.Height = this.facebookAlbumCoverPicture.Image.Height;
                this.Height = Math.Max(this.facebookAlbumSize.Bottom + PADDING, 
                    this.facebookAlbumCoverPicture.Bottom + PADDING);

                LayoutAlbumInformation();

                // Make sure the border stays at the bottom.
                this.facebookAlbumBorderBottom.Top = this.Height - this.facebookAlbumBorderBottom.Height;
            }
        }

        /// <summary>
        /// If the user is creating a new album, this method is used to determine what the visibility
        /// of the album is set to.
        /// </summary>
        /// <returns>
        /// The visibility of the soon-to-be-created album.
        /// </returns>
        private string GetAlbumVisibility()
        {
            string albumVisibilityDescription = 
                this.facebookAlbumVisibilityComboBox.SelectedItem as string;
            string albumVisibility = "everyone"; // Default.

            switch (albumVisibilityDescription)
            {
                case "My Networks":
                    albumVisibility = "networks";
                    break;
                case "Friends of Friends":
                    albumVisibility = "friends-of-friends";
                    break;
                case "Only Friends":
                    albumVisibility = "friends";
                    break;
            }

            return albumVisibility;
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            this.SelectAlbumNameTextBox();
        }

        private void SelectAlbumNameTextBox()
        {
            // Do not reselect the album name if the user is just changing the Visibility ComboBox.
            if (this.album == null && !this.facebookAlbumVisibilityComboBox.Focused)
            {
                // Allow the user to change the name of the album.
                this.facebookAlbumNameTextBox.Show();
                this.facebookAlbumNameTextBox.Focus();
                this.facebookAlbumNameTextBox.Select(0, this.facebookAlbumNameTextBox.Text.Length);
            }
        }

        #endregion
    }
}
